<?php

/*
 +-----------------------------------------------------------------------+
 | plugins/sieverules/rcube_sieve.inc                                    |
 |                                                                       |
 | This file is part of the RoundCube Webmail client                     |
 | Copyright (C) 2008-2009, RoundCube Dev. - Switzerland                 |
 | Licensed under the GNU GPL                                            |
 |                                                                       |
 | PURPOSE:                                                              |
 |	 rcube_sieve class for sieverules operations (using PEAR::Net_Sieve) |
 |                                                                       |
 +-----------------------------------------------------------------------+
 | Author: Aleksander Machniak <alec@alec.pl>                            |
 | Modifications by: Philip Weir                                         |
 |   * Make ruleset name configurable    	                             |
 +-----------------------------------------------------------------------+

 $Id: $

*/

define('SIEVE_ERROR_CONNECTION', 1);
define('SIEVE_ERROR_LOGIN', 2);
define('SIEVE_ERROR_NOT_EXISTS', 3);	// script not exists
define('SIEVE_ERROR_INSTALL', 4);	// script installation
define('SIEVE_ERROR_ACTIVATE', 5);	// script activation
define('SIEVE_ERROR_OTHER', 255);	// other/unknown error

class rcube_sieve {
	var $sieve;
	var $error = false;
	var $list = array();
	var $ruleset;

	public $script;

	public function __construct($username, $password, $host, $port, $usetls, $ruleset) {
		$this->sieve = new Net_Sieve();

		if (PEAR::isError($this->sieve->connect($host, $port, NULL, $usetls)))
			return $this->_set_error(SIEVE_ERROR_CONNECTION);

		if (PEAR::isError($this->sieve->login($username, $password)))
			return $this->_set_error(SIEVE_ERROR_LOGIN);

		$this->ruleset = $ruleset;
		$this->_get_script();
    }

	public function error() {
		return $this->error ? $this->error : false;
    }

	public function save() {
		$script = $this->script->as_text();

		if (!$script)
			$script = '/* empty script */';

		if (PEAR::isError($this->sieve->installScript($this->ruleset, $script)))
			return $this->_set_error(SIEVE_ERROR_INSTALL);

		if (PEAR::isError($this->sieve->setActive($this->ruleset)))
			return $this->_set_error(SIEVE_ERROR_ACTIVATE);

		return true;
	}

	public function get_extensions() {
		if ($this->sieve)
			return $this->sieve->getExtensions();
	}

	private function _get_script() {
		if (!$this->sieve)
			return false;

		$this->list = $this->sieve->listScripts();

		if (PEAR::isError($this->list))
			return $this->_set_error(SIEVE_ERROR_OTHER);

		if (in_array($this->ruleset, $this->list)) {
			$script = $this->sieve->getScript($this->ruleset);

			if (PEAR::isError($script))
				return $this->_set_error(SIEVE_ERROR_OTHER);
		}
      	else {
			$this->_set_error(SIEVE_ERROR_NOT_EXISTS);
			$script = '';
		}

		$this->script = new rcube_sieve_script($script);
	}

	private function _set_error($error) {
		$this->error = $error;
		return false;
	}
}

?>